package com.welink.cmp.meatdata.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.welink.base.statemachine.StateMachine;
import com.welink.base.statemachine.StateMachineFactory;
import com.welink.cmp.convertor.GenericConvertor;
import com.welink.cmp.dao.metadata.dataobject.SwitchDO;
import com.welink.cmp.domain.metadata.enums.ResourceTypeEnum;
import com.welink.cmp.dto.metadata.MetadataIdListQry;
import com.welink.cmp.dto.metadata.switchs.*;
import com.welink.cmp.dto.metadata.switchs.clientobject.SwitchBasicCO;
import com.welink.cmp.dto.metadata.switchs.query.SwitchListByPageQry;
import com.welink.cmp.meatdata.command.SwitchCmdExe;
import com.welink.cmp.statemachine.StateMachineIdContent;
import com.welink.cmp.statemachine.context.CommonCtx;
import com.welink.cmp.statemachine.machine.CommonEvent;
import com.welink.cmp.statemachine.machine.CommonState;
import com.welink.common.dto.PageResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yanwei li create  2024-05-31 18:42
 */
@Service
public class SwitchAppService {

    @Autowired
    private SwitchCmdExe switchCmdExe;

    /**
     * 添加Switch
     *
     * @param addCmd 添加Switch的命令对象
     * @return 新增Switch的ID
     */
    public Long addSwitch(CreateSwitchAddCmd addCmd) {
        return switchCmdExe.save(addCmd);
    }

    /**
     * [修改]配置交换机基础信息
     * @param setupBaseSwitchUpdCmd setupBaseSwitchUpdCmd
     * @return Boolean
     */
    public Boolean setupSwitchBaseInfo( SetupSwitchBaseUpdateCmd setupBaseSwitchUpdCmd){
        SwitchDO switchDO = new SwitchDO();
        BeanUtils.copyProperties(setupBaseSwitchUpdCmd,switchDO);
        if (setupBaseSwitchUpdCmd.isNeedsOperator()){
            switchDO.setUpdateBy(setupBaseSwitchUpdCmd.getOperator());
        }
        final Boolean update = switchCmdExe.update(switchDO);
        return update;
    }
    /**
     * [修改]配置交换机物理信息
     * @param setupSwitchRackUpdCmd setupSwitchRackUpdCmd
     * @return  resultCode = 0 成功 非 0 即错误码和错误信息
     */
    public Boolean setupSwitchRackInfo( SetupSwitchRackUpdateCmd setupSwitchRackUpdCmd){
        SwitchDO switchDO = new SwitchDO();
        BeanUtils.copyProperties(setupSwitchRackUpdCmd,switchDO);
        if (setupSwitchRackUpdCmd.isNeedsOperator()){
            switchDO.setUpdateBy(setupSwitchRackUpdCmd.getOperator());
        }
        final Boolean update = switchCmdExe.update(switchDO);
        return update;
    }

    /**
     * [修改]配置交换机网络信息
     * @param setupSwitchNetUpdCmd 配置交换机
     * @return  resultCode = 0 成功 非 0 即错误码和错误信息
     */
    public Boolean setupSwitchNetInfo( SetupSwitchNetUpdateCmd setupSwitchNetUpdCmd){
        SwitchDO switchDO = new SwitchDO();
        BeanUtils.copyProperties(setupSwitchNetUpdCmd,switchDO);
        if (setupSwitchNetUpdCmd.isNeedsOperator()){
            switchDO.setUpdateBy(setupSwitchNetUpdCmd.getOperator());
        }
        final Boolean update = switchCmdExe.update(switchDO);
        return update;
    }

    public Boolean launchSwitch(LaunchSwitchUpdateCmd launchCmd){
        StateMachine<CommonState, CommonEvent, CommonCtx> stateMachine = StateMachineFactory.get(StateMachineIdContent.common_state_machine_id);
        CommonCtx ctx = CommonCtx.builder()
                .id(launchCmd.getId())
                .operator(launchCmd.getOperator())
                .typeEnum(ResourceTypeEnum.SWITCH)
                .launchedTime(launchCmd.getLaunched_time())
                .build();
        final CommonState state = stateMachine.fireEvent(CommonState.NEW, CommonEvent.MOUNTED, ctx);
        return CommonState.IDLE.getCode() == state.getCode();
    }

    public Boolean shutdownSwitch(ShutdownSwitchUpdateCmd shutdownSwUpdCmd){
        StateMachine<CommonState, CommonEvent, CommonCtx> stateMachine = StateMachineFactory.get(StateMachineIdContent.common_state_machine_id);
        CommonCtx ctx = CommonCtx.builder()
                .id(shutdownSwUpdCmd.getId())
                .typeEnum(ResourceTypeEnum.SWITCH)
                .operator(shutdownSwUpdCmd.getOperator())
                .build();
        final CommonState state = stateMachine.fireEvent(CommonState.IDLE, CommonEvent.UNMOUNTED,ctx);
        return CommonState.NEW.getCode() == state.getCode();
    }

    /**
     * 根据ID获取Switch
     *
     * @param id Switch ID
     * @return Switch业务对象
     */
    public SwitchBasicCO getSwitchById(Long id) {
        return switchCmdExe.queryById(id);
    }

    /**
     * 列表查询Switch
     *
     * @param listQry 列表查询条件
     * @return Switch业务对象列表
     */
    public List<SwitchBasicCO> switchListQuery(MetadataIdListQry listQry) {
        return switchCmdExe.listQuery(listQry);
    }

    /**
     * 分页查询Switch
     *
     * @param pageQry 分页查询条件
     * @return 分页响应，包括Switch列表和分页信息
     */
    public PageResponse<SwitchBasicCO> switchPageQuery(SwitchListByPageQry pageQry) {
        final Page<SwitchDO> pageR = switchCmdExe.pageQuery(pageQry);
        final List<SwitchDO> records = pageR.getRecords();
        final List<SwitchBasicCO> coList = GenericConvertor.toCOList(records,SwitchBasicCO.class);
        return PageResponse.of(coList,pageR.getTotal(),pageR.getSize(),pageR.getCurrent());
    }
}
